package com.corechina.apex_osswords.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.corechina.apex_osswords.common.Response;
import com.corechina.apex_osswords.common.ShiroUtils;
import com.corechina.apex_osswords.common.WeiXinUserUtils;
import com.corechina.apex_osswords.domain.dao.CoreEquipmentMapper;
import com.corechina.apex_osswords.domain.dao.CoreProjectMapper;
import com.corechina.apex_osswords.domain.dao.CoreWorkOrderMapper;
import com.corechina.apex_osswords.domain.entity.CoreEquipment;
import com.corechina.apex_osswords.domain.entity.CoreProject;
import com.corechina.apex_osswords.domain.vo.CoreEquipmentVo;
import com.corechina.apex_osswords.service.ICoreEquipmentService;
import com.corechina.apex_osswords.service.ICoreFilesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 设备表 服务实现类
 * </p>
 *
 * @author cc
 * @since 2022-12-09
 */
@Service
public class CoreEquipmentServiceImpl extends ServiceImpl<CoreEquipmentMapper, CoreEquipment> implements ICoreEquipmentService {

    @Autowired
    private CoreEquipmentMapper coreEquipmentMapper;

    @Autowired
    private CoreProjectMapper coreProjectMapper;

    @Autowired
    private ICoreFilesService iCoreFilesService;

    @Autowired
    private CoreWorkOrderMapper coreWorkOrderMapper;

    @Override
    public Response add(CoreEquipment coreEquipment) {
        coreEquipment.setCreateTime(LocalDateTime.now());
        coreEquipment.setSerialNumber(this.getSerialNumber(coreEquipment.getProjectId()));
        if (coreEquipmentMapper.insert(coreEquipment) > 0) {
            return Response.ok();
        }
        return Response.error();
    }

    @Override
    public Response getList(String name, Integer place, Integer serviceId, Integer type, Integer projectId, Integer pageIndex, Integer pageSize) {
        IPage<CoreEquipmentVo> equipmentVoIPage = coreEquipmentMapper.getList(new Page(pageIndex, pageSize), name, place, serviceId, type, projectId, ShiroUtils.getSeeAll().equals(1) ? null : ShiroUtils.getEnterpriseId());
        return Response.ok(equipmentVoIPage.getRecords(), equipmentVoIPage.getTotal());
    }

    private synchronized String getSerialNumber(Integer projectId) {
        CoreProject coreProject = coreProjectMapper.selectById(projectId);
        String serialNumber = coreEquipmentMapper.getMaxSerialNumber(coreProject.getEnterpriseId(), projectId);
        if (serialNumber == null || !serialNumber.contains(coreProject.getSerialNumber())) {
            return coreProject.getSerialNumber() + "-" + "D001";
        }
        serialNumber = StringUtils.substringAfter(serialNumber, "D");
        serialNumber = coreProject.getSerialNumber() + "-D" + String.format("%03d", Integer.parseInt(serialNumber) + 1);
        if (coreEquipmentMapper.checkSerialNumber(serialNumber) > 0) {
            serialNumber = getSerialNumber(coreProject.getId());
        }

//        serialNumber = coreProject.getSerialNumber();
        return serialNumber;
    }

    @Override
    public Response getOne(Integer id) {
        return Response.ok(coreEquipmentMapper.selectById(id));
    }

    @Override
    public Response getEquipmentForWeiXin(Integer projectId, Integer place, Integer serviceId, Integer type, Integer pageIndex, Integer pageSize) {
        IPage<CoreEquipmentVo> coreEquipmentVoIPage = coreEquipmentMapper.getEquipmentForWeiXin(new Page(pageIndex, pageSize), projectId, place, serviceId, type, projectId != null ? null : WeiXinUserUtils.getUserId());
        for (CoreEquipmentVo coreEquipmentVo : coreEquipmentVoIPage.getRecords()) {
            if (coreEquipmentVo.getImageId() != null) {
                coreEquipmentVo.setImageBase64(iCoreFilesService.getById(coreEquipmentVo.getImageId(), 0).getImageBase64());
            }
        }
        return Response.ok(coreEquipmentVoIPage.getRecords(), coreEquipmentVoIPage.getTotal());

    }

    @Override
    public Response getOneForWeiXin(Integer id) {
        CoreEquipmentVo coreEquipmentVo = coreEquipmentMapper.getOneForWeiXin(id);
        return Response.ok(coreEquipmentVo);
    }

    @Override
    public Response getEquipmentDetailsBySerialNumber(String serialNumber, Integer type) {
        CoreEquipmentVo coreEquipmentVo = coreEquipmentMapper.getEquipmentDetailsBySerialNumber(serialNumber);
        if (coreEquipmentVo == null) {
            return Response.error("设备不存在");
        }
        coreEquipmentVo.setCanRepairs(0);
        if (type.equals(1)) {
            CoreProject coreProject = coreProjectMapper.selectById(coreEquipmentVo.getProjectId());
            if (coreProject.getEnterpriseId().equals(WeiXinUserUtils.getEnterpriseId())) {
                coreEquipmentVo.setCanRepairs(1);
            }
        }
        return Response.ok(coreEquipmentVo);
    }

    public Response delBySerialNumber(String serialNumber) {
        CoreEquipmentVo coreEquipmentVo = coreEquipmentMapper.getEquipmentDetailsBySerialNumber(serialNumber);
        if (coreEquipmentVo == null) {
            return Response.error("设备不存在");
        }
        if (this.checkEquipment(coreEquipmentVo.getId())) {
            return Response.error("设备有关联工单，无法删除");
        }
        if (coreEquipmentMapper.deleteById(coreEquipmentVo.getId()) > 0) {
            return Response.ok();
        }
        return Response.error();
    }

    @Override
    public boolean checkEquipment(Integer id) {
        return coreWorkOrderMapper.checkEquipment(id) > 0;
    }

    @Override
    public Response update(CoreEquipment coreEquipment) {
        coreEquipment.setCreateTime(null);
        coreEquipment.setSerialNumber(null);
        CoreEquipment dbEquipment = coreEquipmentMapper.selectById(coreEquipment.getId());
        if (!dbEquipment.getProjectId().equals(coreEquipment.getProjectId())) {
            coreEquipment.setSerialNumber(this.getSerialNumber(coreEquipment.getProjectId()));
        }
        if (coreEquipmentMapper.updateById(coreEquipment) > 0) {
            return Response.ok();
        }
        return Response.error();
    }
}
